package org.springframework.samples.mvc.basic.account;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.li.controller.DemoController;
import cn.li.controller.Person;

@Controller
@RequestMapping(value="/account2")
public class AccountController2 {

	private Map<Long, Account> accounts = new ConcurrentHashMap<Long, Account>();
	private static final Logger logger = LoggerFactory.getLogger(DemoController.class);
	
	/**
	 * get 请求 http    默认方法  /account
	*/
	@RequestMapping(method=RequestMethod.GET)
	public String getCreateForm(Model model) {
		System.out.println("out-----");
		logger.info("info-----");
		model.addAttribute(new Account());
		return "account2/createForm";
	}
	
	/**
	 * post 请求---form
	 */
	@RequestMapping(method=RequestMethod.POST)
	public String create(@Valid Account account, BindingResult result) {
		if (result.hasErrors()) {
			return "account2/createForm";
		}
		this.accounts.put(account.assignId(), account);
		return "redirect:/account2/" + account.getId();
	}
	
	@RequestMapping(value="{id}", method=RequestMethod.GET)
	public String getView(@PathVariable Long id, Model model,
			@RequestBody Person person) {
		System.out.println("好像已经调用到了.....zemm......."+id);
		System.out.println("好像已经调用到了.....zemm......."+person);
		/**Account account = this.accounts.get(id);
		if (account == null) {
			throw new ResourceNotFoundException(id);
		}
		model.addAttribute(account);*/
		return "account2/view";
	}
	/**
	@RequestMapping(method=RequestMethod.GET,value="{pageNumber}/{pageSize}")
	public String list(@PathVariable int pageNumber,@PathVariable int pageSize,
			@RequestBody Person person) {
		System.out.println("----------------------"+pageNumber);
		return "account/list";
	}*/

	public Map<Long, Account> getAccounts() {
		return accounts;
	}

	public void setAccounts(Map<Long, Account> accounts) {
		this.accounts = accounts;
	}

}
